! function() {
    'use strict';

    if(typeof window.thin == 'object') return;

    /* -----------------------------
     * 获取配置文件别名
     * --------------------------- */
    var scripts = document.getElementsByTagName('script'),
        elem = scripts[scripts.length - 1],
        loader = elem.getAttribute('thin-loader'),
        entran = elem.getAttribute('thin-main'),
        cache = (elem.getAttribute('thin-cache') || 'true') === 'true' ? true : false;

    /* -----------------------------
     * 获取data-*
     * --------------------------- */
    var attr_opt = {};
    for(var i in elem.attributes) {
        if(/^data-/.test(elem.attributes[i].name)) {
            attr_opt[elem.attributes[i].name] = elem.attributes[i].value;
        }
    }

    /* -----------------------------
     * 定义内部变量
     * map_resource    存储已加载的资源路径
     * map_modules     存储已加载的AMD模块
     * thin_count      记录当前模块数量
     * thin_main       记录当前thin.use
     * --------------------------- */
    var map_resource = {},
        map_modules = {
            'data': attr_opt
        },
        thin_count = 0,
        thin_main = null;

    /* -----------------------------
     * 返回新实例
     * --------------------------- */
    function new_map_modules() {
        return map_modules;
    }

    /* -----------------------------
     * 框架别名
     * --------------------------- */
    var thin = {
        data: attr_opt,

        path: function(pathArrs) {
            pathArrs.length >= 1 ? pathMap = pathArrs : null;
            return this;
        },

        define: function(modules) {
            var self = thin;
            ++thin_count;
            if(typeof modules == 'function') {
                var callback = function(model, opts) {
                    --thin_count;
                    if(typeof model == 'string') {
                        self[model] = opts;
                        map_modules[model] = opts;
                    } else if(typeof model == 'function') {
                        model();
                    }
                    thin_complete();
                }
                modules.apply(new_map_modules(), [callback]);
            } else if(typeof modules == 'undefined') {
                --thin_count;
                thin_complete();
            }
        },

        use: function(main) {
            if(thin_main != null) return;
            thin_main = main;
            thin_complete();
        },

        /* -----------------------------
         * 导入[模板|模块|文件]
         * 格式仅为[JS|CSS]
         * ------------------------
         * params:
         * arguments[0]    路径
         * --------------------------- */
        imports: function() {
            var resource = arguments[0] || null;
            if(!resource) return;

            // 判断资源是否加载过
            if(typeof map_resource[resource] == 'boolean') return;
            map_resource[resource] = false;

            var reg = new RegExp();
            reg = /\.js/gi;
            if(reg.test(resource)) {
                loaderJS.apply(new_map_modules(), arguments);
            }

            reg = /\.css/gi;
            if(reg.test(resource)) {
                loaderCSS.apply(new_map_modules(), arguments);
            }
        }
    };

    /* -----------------------------
     * use函数
     * --------------------------- */
    function thin_complete() {
        if(thin_count == 0) {
            //map_modules['data'] = thin.data;
            (typeof thin_main == 'function') && thin_main.apply(new_map_modules());
        }
    }

    /* -----------------------------
     * 获取容器
     * --------------------------- */
    var head = document.getElementsByTagName('head')[0];

    /* -----------------------------
     * 加载js
     * --------------------------- */
    function loaderJS(src, callback) {
        thin.define(function(exports) {
            var node = document.createElement('script');
            node.src = src + '?_t=' + new Date().getTime();
            node.onload = function() {
                exports(callback);
            };
            head.appendChild(node);
        });
    }

    /* -----------------------------
     * 加载css
     * --------------------------- */
    function loaderCSS(resource) {
        thin.define(function(exports) {
            var node = document.createElement('link');
            node.type = "text/css";
            node.rel = "stylesheet";
            node.href = resource;
            node.onload = exports;
            head.appendChild(node);
        });
    }

    /* -----------------------------
     * 引入加载器
     * 引入主文件
     * --------------------------- */
    (loader && loader != '') && thin.imports(loader);
    (entran && entran != '') && thin.imports(entran);

    /* -----------------------------
     * 配置全局变量
     * --------------------------- */
    window.thin = thin;
}();